package com.cbtx.tablet.util

import android.os.Environment
import android.text.TextUtils
import com.cbtx.tablet.bean.GoodsItemBean
import com.cbtx.tablet.bean.MainMenuBean2
import com.cbtx.utils.Base64Util
import com.cbtx.utils.FileUtil
import java.io.*

/**
 * author : joker.peng
 * e-mail : joker@fiture.com
 * date   : 2021/7/9 2:45 AM
 * desc   :
 */
object DataCacheUtil {
    /**
     * 缓存收藏宝首页数据
     *
     * @param
     * @param datas
     */
    fun saveGoodsTreasureList(datas: List<GoodsItemBean>?) {
        if (datas == null) {
            return
        }
        val file = File(FileUtil.getCacheDir().toString() + "/", "GoodsTreasureList.json")
        if (file.exists()) {
            file.delete()
        }
        try {
            file.createNewFile()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        val base64 = Base64Util.objectTo64Str(datas)
        saveToSDCard(file, base64)
    }

    /**
     * 获取收藏宝首页数据
     *
     * @param
     * @param
     */
    fun readGoodsTreasureList(): List<GoodsItemBean> {
        val mDatas = mutableListOf<GoodsItemBean>()
        val file = File(FileUtil.getCacheDir().toString() + "/", "GoodsTreasureList.json")
        if (!file.exists()) {
            return mDatas
        }
        val base64 = readTextFile(file.path)
        if (TextUtils.isEmpty(base64)) {
            return mDatas
        }
        try {
            mDatas.addAll(Base64Util.base64ToObject(base64) as MutableList<GoodsItemBean>)
        } catch (e: Exception) {
        }
        return mDatas
    }

    fun saveAppList(datas: List<MainMenuBean2>) {
        val file = File(FileUtil.getCacheDir().toString() + "/", "AppList.json")
        if (file.exists()) {
            file.delete()
        }
        try {
            file.createNewFile()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        val base64 = Base64Util.objectTo64Str(datas)
        saveToSDCard(file, base64)
    }

    fun readAppList(): List<MainMenuBean2>? {
        val mDatas = mutableListOf<MainMenuBean2>()
        val file = File(FileUtil.getCacheDir().toString() + "/", "AppList.json")
        if (!file.exists()) {
            return mDatas
        }
        val base64 = readTextFile(file.path)
        if (TextUtils.isEmpty(base64)) {
            return mDatas
        }
        try {
            mDatas.addAll(Base64Util.base64ToObject(base64) as MutableList<MainMenuBean2>)
        } catch (e: Exception) {
        }
        return mDatas
    }

    /**
     * 缓存认证首页数据
     *
     * @param
     * @param datas
     */
    fun saveGoodsAuthList(datas: List<GoodsItemBean?>?) {
        if (datas == null) {
            return
        }
        val file = File(FileUtil.getCacheDir().toString() + "/" , "GoodsAuthList.json")
        if (file.exists()) {
            file.delete()
        }
        try {
            file.createNewFile()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        val base64 = Base64Util.objectTo64Str(datas)
        saveToSDCard(file, base64)
    }

    fun readGoodsAuthList(): List<GoodsItemBean> {
        val mDatas = mutableListOf<GoodsItemBean>()
        val file = File(FileUtil.getCacheDir().toString() + "/" , "GoodsAuthList.json")
        if (!file.exists()) {
            return mDatas
        }
        val base64 = readTextFile(file.path)
        if (TextUtils.isEmpty(base64)) {
            return mDatas
        }
        try {
            mDatas.addAll(Base64Util.base64ToObject(base64) as MutableList<GoodsItemBean>)
        } catch (e: Exception) {
        }
        return mDatas
    }

    /**
     * 保存收藏宝详情数据
     *
     * @param mData
     */
    fun saveGoodsTreasureDetail(mData: GoodsItemBean) {
        val file = File(FileUtil.getCacheDir(), "GoodsTreasureList")
        if (!file.exists()) {
            file.mkdir()
        }
        val file2 = File(file.absolutePath, "GoodsTreasure" + mData.serialNumber)
        if (file2.exists()) {
            file2.delete()
        }
        try {
            file2.createNewFile()
        } catch (e: IOException) {
            e.printStackTrace()
        }

//        String json = new Gson().toJson(mData);
        val base64 = Base64Util.objectTo64Str(mData)
        saveToSDCard(file2, base64)
    }

    /**
     * 获取收藏宝详情数据
     *
     * @param serialNumber
     * @return
     */
    fun getGoodsTreasureDetail(serialNumber: String): GoodsItemBean? {
        var renZhengItem = GoodsItemBean()
        val file = File(FileUtil.getCacheDir(), "GoodsTreasureList")
        if (!file.exists()) {
            file.mkdir()
        }
        val file2 = File(file.absolutePath, "GoodsTreasure$serialNumber")
        val base64 = readTextFile(file2.path)
        if (TextUtils.isEmpty(base64)) {
            return renZhengItem
        }
        try {
            renZhengItem = Base64Util.base64ToObject(base64) as GoodsItemBean
        } catch (e: Exception) {
        }
        return renZhengItem
    }

    //-----------------------------------------------------------------
    fun saveToSDCard(file: File, content: String?) {
        val en = Environment.getExternalStorageState()
        if (!file.exists()) {
            file.mkdir()
        }
        //获取SDCard状态,如果SDCard插入了手机且为非写保护状态
        if (en == Environment.MEDIA_MOUNTED) {
            try {
                val out: OutputStream = FileOutputStream(file)
                out.write(content?.toByteArray())
                out.close()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        } else {
            //提示用户SDCard不存在或者为写保护状态
        }
    }

    /**
     * 从本地读取json
     */
    fun readTextFile(filePath: String?): String {
        val sb = StringBuilder()
        try {
            val file = File(filePath)
            var `in`: InputStream? = null
            `in` = FileInputStream(file)
            var tempbyte: Int
            while (`in`.read().also { tempbyte = it } != -1) {
                sb.append(tempbyte.toChar())
            }
            `in`.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return sb.toString()
    }
}